import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router= new Router({
  routes: [
    {
      path: '/home',
      component: ()=>import('@/pages/Home')
    },
    {
      path: '/singer',
      component: ()=>import('@/pages/Singer')
    },
    {
      path: '/songList',
      component: ()=>import('@/pages/SongList')
    },
    {
      path: '/myMusic',
      component: ()=>import('@/pages/MyMusic')
    },
    {
      path: '/search',
      component: ()=>import('@/pages/Search')
    },
    {
      path: '/songInfo',
      component: ()=>import('@/pages/SongInfo')
    },
    {
      path: '/login',
      component: ()=>import('@/pages/Login')
    },
    {
      path: '/register',
      component: ()=>import('@/pages/Register')
    },
    {
      path: '/setting',
      component: ()=>import('@/pages/Setting')
    },
    {
      path: '/singerInfo/:id',
      component: ()=>import('@/pages/SingerInfo')
    },
    {
      path: '/songListInfo/:id',
      component: ()=>import('@/pages/SongListInfo')
    },
    {
      path:'*',
      redirect:'/home'
    }

  ],
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})
//配置全局前置守卫
router.beforeEach( (to,from,next)=>{
  //to:跳转到哪个地方,from:来自哪个地方，next：放行函数 next(path)放行到指定路由
  //获取Token
  let Token=localStorage.getItem('Token')
  if (Token){
    if (to.path=='/login'){
      next('/home?name=首页')
    }
    next()
  }else {
    if (to.path=='/myMusic'){
      next('/login?name=登录')
    }
    next()
  }
})




export default router
